
goog.provide('esn.model.StrainedFriendSurveyModel');

goog.require('esn.model.StudentModel');
goog.require('goog.object');


/**
 * @param {Object|esn.model.StudentModel} model A JSON model of a friend
 *     survey or a student model of the friend in the survey.
 * @constructor
 */
esn.model.StrainedFriendSurveyModel = function(model) {
  // If this is a number then we assume this is a new survey model and that
  // this number is the friend id.
  if (model instanceof esn.model.StudentModel) {
    model = {'friend': model};
  } else if ('friend' in model) {
    model['friend'] = new esn.model.StudentModel(model['friend']);
  }

  this.model_ = model;

  // Verify that the student_id and friend are set.
  if (!this.getFriend()) {
    throw new Error('The "friend" field must be set in the model.');
  }
};

esn.model.StrainedFriendSurveyModel.prototype.getId = function() {
  return this.model_['id'];
};

esn.model.StrainedFriendSurveyModel.prototype.getStudentId = function() {
  return this.model_['student_id'];
};

esn.model.StrainedFriendSurveyModel.prototype.getFriend = function() {
  return this.model_['friend'];
};

esn.model.StrainedFriendSurveyModel.prototype.getRelationshipLevel =
    function() {
  return this.model_['relationship_level'];
};

esn.model.StrainedFriendSurveyModel.prototype.setRelationshipLevel = function(
    relationshipLevel) {
  this.model_['relationship_level'] = relationshipLevel;
};

esn.model.StrainedFriendSurveyModel.prototype.toObject = function() {
  var model = goog.object.clone(this.model_);
  model['friend'] = this.getFriend().toObject();
  return model;
};

esn.model.StrainedFriendSurveyModel.prototype.mergeFrom = function(model) {
  // Set any fields present in the new model that are not in the current model.
  // Note: No existing values are overridden.
  goog.object.forEach(model.toObject(), function(value, key) {
    if (!(key in this.model_)) {
      if (key == 'friend') {
        this.model_[key] = new esn.model.StudentModel(value);
      } else {
        this.model_[key] = value;
      }
    }
  }, this);
};
